Pull out a little more char* from our core infrastructure. Add shims to turn down...
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 11 Sep 2013 05:06:34 +0000 (05:06 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 11 Sep 2013 05:06:34 +0000 (05:06 +0000)
Tweak reference files to deal with more correct(?) ordering in edge cases.
Start sketching a shim layer for shortname, description, and text, but increasingly I'm of the opinion that our only options are either do it all in one submit (eeek!) or to add additional members of struct waypt that are allocated/copied at waypt_add time so we can then at least separate readers and writers.  Instinct tells me the latter will result in harsh punishment as we'll have members that are out of sync and it will be difficult to flush them out.  So substantial parts of this CL may get tossed.

gpsbabel/defs.h
gpsbabel/garmin_txt.cc
gpsbabel/jeeps/gpsmath.cc
gpsbabel/ozi.cc
gpsbabel/reference/garmin_txt-uni.csv
gpsbabel/reference/garmin_txt.txt
gpsbabel/route.cc
gpsbabel/util.cc
gpsbabel/waypt.cc
gpsbabel/wfff_xml.cc
gpsbabel/xol.cc

index b72cf1f26ce40211b9cf6478fe6f4c5c38cc032f..7edecc2aefa829fe8787288e78fe65f2ba48e934 100644 (file)
@@ -437,6 +437,26 @@ const global_trait* get_traits();
  * be truncated, edited, or otherwise trimmed should be done on the
  * way to the target.
  */
+#if NEW_STRINGS
+class String {
+ public:
+  String() :
+    s_(NULL) {}
+  bool isEmpty() {
+    return s_ && *s_;
+  }
+  operator char*() const {
+    return s_;
+  }
+  char* operator=(char* s) {
+    s_ = s;
+    return s_;
+  }
+  char* s_;
+};
+#else
+typedef char* String;
+#endif
 
 class waypoint
 {
@@ -447,9 +467,11 @@ public:
     altitude(-99999999.0),
     depth(0),
     proximity(0),
+#if !NEW_STRINGS
     shortname(NULL),
     description(NULL),
     notes(NULL),
+#endif
     route_priority(0),
     hdop(0),
     vdop(0),
@@ -496,20 +518,20 @@ public:
    * minimum length for shortname is 6 characters for NMEA units,
    * 8 for Magellan and 10 for Vista.   These are only guidelines.
    */
-  char* shortname;
+  String shortname;
   /*
    * description is typically a human readable description of the
    * waypoint.   It may be used as a comment field in some receivers.
    * These are probably under 40 bytes, but that's only a guideline.
    */
-  char* description;
+  String description;
   /*
    * notes are relatively long - over 100 characters - prose associated
    * with the above.   Unlike shortname and description, these are never
    * used to compute anything else and are strictly "passed through".
    * Few formats support this.
    */
-  char* notes;
+  String notes;
 
   /* TODO: UrlLink should probably move to a "real" class of its own.
    */
@@ -963,8 +985,18 @@ void debug_mem_close();
 
 FILE* xfopen(const char* fname, const char* type, const char* errtxt);
 
-int case_ignore_strcmp(const char* s1, const char* s2);
-int case_ignore_strncmp(const char* s1, const char* s2, int n);
+// FIXME: case_ignore_strcmp() and case_ignore_strncmp() should probably
+// just be replaced at the call sites.  These shims are just here to make
+// them more accomidating of QString input.
+inline int
+case_ignore_strcmp(const QString& s1, const QString& s2) {
+  return QString::compare(s1, s2, Qt::CaseInsensitive);
+}
+// In 95% of the callers, this could be s1.startsWith(s2)...
+inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n) {
+  return s1.left(n).compare(s2, Qt::CaseInsensitive);
+}
+
 int str_match(const char* str, const char* match);
 int case_ignore_str_match(const char* str, const char* match);
 char* strenquote(const char* str, const char quot_char);
@@ -1173,4 +1205,8 @@ int color_to_bbggrr(const char* cname);
 #define unknown_alt    -99999999.0
 #define unknown_color  -1
 
+// TODO: this is a (probably temporary) shim for the C->QString conversion.
+// It's here instead of gps to avoid C/C++ linkage issues.
+int32_t GPS_Lookup_Datum_Index(const QString& n);
+
 #endif /* gpsbabel_defs_h_included */
index 60dc628a232765d5b09068faeeb1696e6bc5b320..5120c2a76c4a6fcd1bb5dc7957204afece9c61c3 100644 (file)
@@ -227,7 +227,7 @@ sort_waypt_cb(const void* a, const void* b)
   const waypoint* wa = *(waypoint**)a;
   const waypoint* wb = *(waypoint**)b;
 
-  return case_ignore_strcmp(wa->shortname, wb->shortname);
+  return case_ignore_strcmp(QString::fromLatin1(wa->shortname), QString::fromLatin1(wb->shortname));
 }
 
 
index a5e9de25d869c234c86b1e0b9fb920a5763ad8ba..ecb34c74df99f11bb742b81d6846efafc8e891ad 100644 (file)
@@ -2572,6 +2572,11 @@ int32 GPS_Lookup_Datum_Index(const char* n)
   return -1;
 }
 
+int32 GPS_Lookup_Datum_Index(const QString& n)
+{
+  return GPS_Lookup_Datum_Index(CSTR(n));
+}
+
 const char*
 GPS_Math_Get_Datum_Name(const int datum_index)
 {
index 736027867fdc1d5c1b50d975637a74c835f8452c..2adc9ec3f18da3e9e085d6590152472f0382efdf 100644 (file)
@@ -720,15 +720,14 @@ ozi_parse_routeheader(int field, char* str, waypoint* wpt_tmp)
 static void
 data_read(void)
 {
-  char* buff;
+  QString buff;
   char* s = NULL;
   char* trk_name = NULL;
   waypoint* wpt_tmp;
   int i;
   int linecount = 0;
 
-  while ((buff = gbfgetstr(file_in))) {
-
+  while ((buff = gbfgetstr(file_in)), !buff.isNull()) {
     if ((linecount++ == 0) && file_in->unicode) {
       cet_convert_init(CET_CHARSET_UTF8, 1);
     }
@@ -738,36 +737,37 @@ data_read(void)
      * to attempt to divine the data type we are parsing
      */
     if (linecount == 1) {
-      if (strstr(buff, "Track Point") != NULL) {
+      if (buff.contains("Track Point")) {
         trk_head = route_head_alloc();
         track_add_head(trk_head);
         ozi_objective = trkdata;
-      } else if (strstr(buff, "Route File") != NULL) {
+      } else if (buff.contains("Route File")) {
         ozi_objective = rtedata;
       } else {
         ozi_objective = wptdata;
       }
     } else if (linecount == 2) {
       datum = GPS_Lookup_Datum_Index(buff);
+
       if (datum < 0) {
-        fatal(MYNAME ": Unsupported datum '%s'.\n", buff);
+        fatal(MYNAME ": Unsupported datum '%s'.\n", CSTR(buff));
       }
     } else if (linecount == 3) {
-      if (case_ignore_strncmp(buff, "Altitude is in ", 15) == 0) {
-        char* unit = &buff[15];
-        if (case_ignore_strncmp(unit, "Feet", 4) == 0) {
+      if (buff.startsWith( "Altitude is in ", Qt::CaseInsensitive)) {
+        QString unit = buff.mid(15);
+        if (unit.startsWith("Feet", Qt::CaseInsensitive)) {
           altunit = 'f';
           alt_scale = FEET_TO_METERS(1.0);
-        } else if (case_ignore_strncmp(unit, "Meter", 5) == 0) {
+        } else if (unit.startsWith("Meter", Qt::CaseInsensitive)) {
           altunit = 'm';
           alt_scale = 1.0;
         } else {
-          fatal(MYNAME ": Unknown unit (%s) used by altitude values!\n", unit);
+          fatal(MYNAME ": Unknown unit (%s) used by altitude values!\n", CSTR(unit));
         }
       }
     } else if ((linecount == 5) && (ozi_objective == trkdata)) {
       int field = 0;
-      s = csv_lineparse(buff, ",", "", linecount);
+      s = csv_lineparse(CSTR(buff), ",", "", linecount);
       while (s) {
         field ++;
         if (field == 4) {
@@ -777,13 +777,13 @@ data_read(void)
       }
     }
 
-    if ((strlen(buff)) && (strstr(buff, ",") != NULL)) {
+    if (buff.contains(',')) {
       bool ozi_fsdata_used = false;
       ozi_fsdata* fsdata = ozi_alloc_fsdata();
       wpt_tmp = waypt_new();
 
       /* data delimited by commas, possibly enclosed in quotes.  */
-      s = buff;
+      s = xstrdup(CSTR(buff));
       s = csv_lineparse(s, ",", "", linecount);
 
       i = 0;
index 51ba3799b70b1e903e3038a33466b7fcb9af9429..9fa7aea5ba639698f80049a7458de007471efa19 100644 (file)
@@ -10,6 +10,6 @@ No,Latitude,Longitude,Name,Altitude,Description,Symbol,Date,Time,URL,Facility,Ci
 9,38.631995,-3.174055,"GC_X",,"Dummy airport (Spain)","Airport",2006/03/28,01:42:01,,"FAC2","CITY2","Canary Island"\r
 10,50.493667,12.107150,"Jahnstrasse",,"Jahnstrasse 11","Flag, Red",2006/03/31,21:48:22,,,,\r
 11,46.387606,3.498277,"LF_X",,"Dummy airport (France)","Airport",2006/03/28,01:40:32,,"FAC3","CITY3","France"\r
-12,50.493834,12.106100,"Liebknechtstrasse",,"Liebknechtstrasse 90","Waypoint",2006/03/31,21:49:30,,,,\r
-13,43.314550,12.161554,"LI_X",,"Dummy airport (Italy)","Heliport",2006/03/28,01:43:25,,"FAC4","CITY4","Italy"\r
+12,43.314550,12.161554,"LI_X",,"Dummy airport (Italy)","Heliport",2006/03/28,01:43:25,,"FAC4","CITY4","Italy"\r
+13,50.493834,12.106100,"Liebknechtstrasse",,"Liebknechtstrasse 90","Waypoint",2006/03/31,21:49:30,,,,\r
 14,50.492616,12.105448,"NARVA",391.0,"Start","Flag, Green",2006/03/31,21:49:26,"http://www.narva-light.de",,,\r
index dae88c25be40f0fb9f12ed52ea036fbeda4eb0d6..e11091ffef22c07f09ae6002b9b52aab76eb0daf 100644 (file)
@@ -14,8 +14,8 @@ Waypoint      ED_X    Dummy airport (Germany) Airport N51 53.627961650 E12 58.676564991
 Waypoint       GC_X    Dummy airport (Spain)   Airport N38 37.919719778 W3 10.443304181                                        Symbol & Name   Unknown Airport FAC2    CITY2           Canary Island   28/03/2006 01:42:01             \r
 Waypoint       Jahnstrasse     Jahnstrasse 11  User Waypoint   N50 29.619998485 E12 06.429000869                                       Symbol & Description    Unknown Flag, Red                                       31/03/2006 21:48:22             \r
 Waypoint       LF_X    Dummy airport (France)  Airport N46 23.256332763 E3 29.896638617                                        Symbol & Name   Unknown Airport FAC3    CITY3           France  28/03/2006 01:40:32             \r
-Waypoint       Liebknechtstrasse       Liebknechtstrasse 90    User Waypoint   N50 29.630041681 E12 06.366015896                                       Symbol & Name   Unknown Waypoint                                        31/03/2006 21:49:30             \r
 Waypoint       LI_X    Dummy airport (Italy)   Airport N43 18.873018846 E12 09.693240859                                       Symbol & Name   Unknown Heliport        FAC4    CITY4           Italy   28/03/2006 01:43:25             \r
+Waypoint       Liebknechtstrasse       Liebknechtstrasse 90    User Waypoint   N50 29.630041681 E12 06.366015896                                       Symbol & Name   Unknown Waypoint                                        31/03/2006 21:49:30             \r
 Waypoint       NARVA   Start   User Waypoint   N50 29.556958191 E12 06.326884143       391 m                           Symbol  Unknown Flag, Green                                     31/03/2006 21:49:26     http://www.narva-light.de       Category 15\r
 \r
 \r
index 2abce768c55413d7011d7c8b35bc3dab4be76628..aa23f5efd37edab6fcbbfc27594247303e0d1cdf 100644 (file)
@@ -180,7 +180,13 @@ any_route_add_wpt(route_head* rte, waypoint* wpt, int* ct, int synth, const char
     (*ct)++;
   }
   if (synth && !wpt->shortname) {
+#if NEW_STRINGS
+    char *t;
+    xasprintf(&t, "%s%0*d", namepart, number_digits, *ct);
+    wpt->shortname = t;
+#else
     xasprintf(&wpt->shortname,"%s%0*d", namepart, number_digits, *ct);
+#endif
     wpt->wpt_flags.shortname_is_synthetic = 1;
   }
   update_common_traits(wpt);
index 581a8704e1e68f02e55e08fe9c6d6168e1001f0f..34dce8d12c4f855dc43bffbe6d6ded8a8795f289 100644 (file)
@@ -420,36 +420,6 @@ lrtrim(char* buff)
   return buff;
 }
 
-/*
- *   Like strcmp, but case insensitive.  Like Berkeley's strcasecmp.
- */
-
-int
-case_ignore_strcmp(const char* s1, const char* s2)
-{
-  for (; toupper(*s1) == toupper(*s2); ++ s1, ++s2) {
-    if (*s1 == 0) {
-      return 0;
-    }
-  }
-  return (toupper(*s1) < toupper(*s2)) ? -1 : +1;
-
-}
-
-int
-case_ignore_strncmp(const char* s1, const char* s2, int n)
-{
-  int rv = 0;
-
-  while (n && ((rv = toupper(*s1) - toupper(*s2)) == 0)
-         && *s1) {
-    s1++;
-    s2++;
-    n--;
-  }
-  return rv;
-}
-
 /*
  * compare str with match
  * match may contain wildcards "*" and "?"
index e69cffc7a7f18814d5bb837c7244dc3895609896..582c29928380384690cad06842220aa1f5ce285d 100644 (file)
@@ -160,12 +160,12 @@ waypt_add(waypoint* wpt)
   }
 
   if ((wpt->latitude < -90) || (wpt->latitude > 90.0))
-    fatal("%s: Invalid latitude %f in waypoint %s.\n",
+    fatal("%s: Invalid latitude %f in waypoint '%s'.\n",
           wpt->session->name,
-          lat_orig, wpt->shortname ? wpt->shortname : "");
+          lat_orig, wpt->shortname);
   if ((wpt->longitude < -180) || (wpt->longitude > 180.0))
-    fatal("Invalid longitude %f in waypoint %s.\n",
-          lon_orig, wpt->shortname ? wpt->shortname : "");
+    fatal("Invalid longitude %f in waypoint '%s'.\n",
+          lon_orig, wpt->shortname);
 
   /*
    * Some input may not have one or more of these types so we
@@ -179,7 +179,9 @@ waypt_add(waypoint* wpt)
       wpt->shortname = xstrdup(wpt->notes);
     } else {
       /* Last ditch:  make up a name */
-      xasprintf(&wpt->shortname, "WPT%03d", waypt_ct);
+      char *sn;
+      xasprintf(&sn, "WPT%03d", waypt_ct);
+      wpt->shortname = sn;
     }
   }
 
index 65be3a8f5995fecc455e9cb97c03a21fcaf56c97..4e57e5c8196cdb068390df965e63dcc40e700fba 100644 (file)
@@ -227,14 +227,16 @@ void wfff_e(const char* args, const QXmlStreamAttributes* unused)
     wpt_tmp->altitude = unknown_alt;
     wpt_tmp->fix = fix_unknown;
 
-    if (case_ignore_strncmp(ap_wep,"On",2)==0) {
-      if (case_ignore_strncmp(ap_type,"AP",2)==0) {
+    QString ap_wep_(ap_wep);
+    QString ap_type_(ap_type);
+    if (ap_wep_.startsWith("on", Qt::CaseInsensitive)) {
+      if (ap_type_.startsWith("AP", Qt::CaseInsensitive)) {
         wpt_tmp->icon_descr = aicicon; /* Infra Closed */
       } else {
         wpt_tmp->icon_descr = ahcicon; /* AdHoc Closed */
       }
     } else {
-      if (case_ignore_strncmp(ap_type,"AP",2)==0) {
+      if (ap_type_.startsWith("AP", Qt::CaseInsensitive)) {
         wpt_tmp->icon_descr = aioicon; /* Infra Open */
       } else {
         wpt_tmp->icon_descr = ahoicon; /* AdHoc Open */
index de290024c7c3d2dff19743cdc435303bebc41032..32169c17e85c287812a1e52ef9f60bcca5e7c1a4 100644 (file)
@@ -227,7 +227,7 @@ static void
 xol_waypt_disp_cb(const waypoint* wpt)
 {
   double x, y;
-  char* name;
+  const char* name;
 
   name = wpt->shortname;
   if ((name == NULL) || (*name == '\0') || global_opts.synthesize_shortnames) {